package com.kofigyan.stateprogressbarsample.not_stateprogressbar.custom;

import com.kofigyan.stateprogressbar.utils.PxUtils;
import com.kofigyan.stateprogressbarsample.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.*;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import java.io.IOException;

public class ActionBar extends DependentLayout {

    private DirectionalLayout llActionbar;
    private DirectionalLayout llLeft;
    private DirectionalLayout llCenter;
    private DirectionalLayout llRight;

    /**
     * 总布局
     */
    private Component rootView;

    /**
     * 字体颜色
     */
    private int leftTextColor = ResourceTable.Color_black;
    private int centerTextColor = ResourceTable.Color_black;
    private int rightTextColor = ResourceTable.Color_black;

    /**
     * 字体大小
     */
    private int lefTextSize = PxUtils.fp2px(getContext(),16);
    private int centerTextSize = PxUtils.fp2px(getContext(),18);
    private int rightTextSize = PxUtils.fp2px(getContext(),16);

    /**
     * 中间显示字体是否加粗
     */
    private boolean isCenterTextBold;

    public ActionBar(Context context) {
        this(context, null);
    }

    public ActionBar(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public ActionBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(context);
    }

    public void setRightTextColor(int rightTextColor) {
        this.rightTextColor = rightTextColor;
    }

    public void setLeftTextColor(int leftTextColor) {
        this.leftTextColor = leftTextColor;
    }

    public void setCenterTextColor(int centerTextColor) {
        this.centerTextColor = centerTextColor;
    }

    public void setLefTextSize(int lefTextSize) {
        this.lefTextSize = PxUtils.fp2px(getContext(), lefTextSize);
    }


    public void setCenterTextSize(int centerTextSize) {
        this.centerTextSize = PxUtils.fp2px(getContext(), centerTextSize);
    }

    public void setRightTextSize(int rightTextSize) {
        this.rightTextSize = PxUtils.fp2px(getContext(), rightTextSize);
    }

    /**
     * 是否加粗中间显示的字体
     *
     * @param centerTextBold 是否加粗
     */
    public void setCenterTextBold(boolean centerTextBold) {
        isCenterTextBold = centerTextBold;
    }

    /**
     * 获取跟布局
     */
    public Component getActionBarRootView() {
        return rootView;
    }

    /**
     * 初始化界面
     *
     * @param context
     */
    private void initView(Context context) {
        rootView = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_action_bar, this, true);
        llActionbar = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_actionbar);
        llLeft = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_actionbar_left);
        llCenter = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_actionbar_centre);
        llRight = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_actionbar_right);
        showBackImg(true);
    }

    /**
     * 设置左边图标
     *
     * @param res 图片资源
     * @param l   监听器
     */
    public void setLeftIcon(int res, ClickedListener l) {
        llLeft.setVisibility(Component.VISIBLE);
        llLeft.removeAllComponents();
        Image ivLeft = getImageView();
        ivLeft.setPixelMap(res);
        ivLeft.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
        llLeft.addComponent(ivLeft);
        if (l != null) {
            llLeft.setClickedListener(l);
        }
    }

    /**
     * 设置中间图标
     *
     * @param res 图片资源
     * @param l   监听器
     */
    public void setCenterIcon(int res, ClickedListener l) {
        llCenter.setVisibility(Component.VISIBLE);
        llCenter.removeAllComponents();
        Image center = getImageView();
        center.setPixelMap(res);
        llCenter.addComponent(center);
        llCenter.setClickedListener(l);
    }

    /**
     * 设置右边图标
     *
     * @param res 图片资源
     * @param l   监听器
     */
    public void setRightIcon(int res, ClickedListener l) {
        llRight.setVisibility(Component.VISIBLE);
        llRight.removeAllComponents();
        Image right = getImageView();
        right.setPixelMap(res);
        llRight.addComponent(right);
        if (l != null) {
            llRight.setClickedListener(l);
        }
    }

    /**
     * 设置左边文字
     * @param text  文字
     * @param l     监听器
     */
    public void setLeftText(String text, ClickedListener l) {
        llLeft.setVisibility(Component.VISIBLE);
        llLeft.removeAllComponents();
        Text left = getTextView();
        left.setText(text);
        try {
            Color color = new Color(getResourceManager().getElement(leftTextColor).getColor());
            left.setTextColor(color);
            left.setTextSize(lefTextSize);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        if (l != null) {
            llLeft.setClickedListener(l);
        }
    }

    /**
     * 设置左边文字
     *
     * @param text 文字
     */
    public void setLeftText(String text) {
        setLeftText(text, null);
    }

    public void setCenterText(String text, ClickedListener l) {

        llCenter.setVisibility(Component.VISIBLE);
        llCenter.removeAllComponents();
        Text center = getTextView();
        try {
            center.setTextSize(centerTextSize);
            center.setMaxTextLines(1);
            center.setText(text);
            center.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
            Color color = new Color(getResourceManager().getElement(centerTextColor).getColor());
            center.setTextColor(color);
            if (isCenterTextBold) {
                // 设置字体粗体
                Font font = new Font.Builder("myfont")
                        .setWeight(Font.BOLD)
                        .build();
                center.setFont(font);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        llCenter.addComponent(center);
        if (l != null) {
            llCenter.setClickedListener(l);
        }
    }

    /**
     * 设置中间文字
     *
     * @param text 文字
     */
    public void setCenterText(String text) {
        setCenterText(text, null);
    }

    /**
     * 设置右边文字
     *
     * @param text 文字
     * @param l    监听器
     */
    public void setRightText(String text, ClickedListener l) {
        llRight.setVisibility(Component.VISIBLE);
        llRight.removeAllComponents();
        Text tvRight = getTextView();
        tvRight.setText(text);
        tvRight.setTextSize(rightTextSize);
        try {
            Color color = new Color(getResourceManager().getElement(rightTextColor).getColor());
            tvRight.setTextColor(color);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        llRight.addComponent(tvRight);
        if (l != null) {
            llRight.setClickedListener(l);
        }
    }

    /**
     * 设置右边文字
     *
     * @param text 文字
     */
    public void setRightText(String text) {
        setRightText(text, null);
    }

    /**
     * 得到右边的布局
     *
     * @return DirectionalLayout
     */
    public DirectionalLayout getRightView() {
        return llRight;
    }

    /**
     * 得到中间的布局
     *
     * @return DirectionalLayout
     */
    public DirectionalLayout getCenterView() {
        return llCenter;
    }

    /**
     * 得到左边的布局
     *
     * @return DirectionalLayout
     */
    public DirectionalLayout getLeftView() {
        return llLeft;
    }

    /**
     * 设置左边布局
     *
     * @param v v
     */
    public void setLeftView(Component v) {
        llLeft.setVisibility(VISIBLE);
        llLeft.removeAllComponents();
        llLeft.addComponent(v);
    }

    /**
     * 设置中间布局
     *
     * @param v v
     */
    public void setCenterView(Component v) {
        llCenter.setVisibility(VISIBLE);
        llCenter.removeAllComponents();
        llCenter.addComponent(v);
    }

    /**
     * 设置右边的view
     *
     * @param v v
     */
    public void setRightView(Component v) {
        setRightView(v, null);
    }

    public void setRightView(Component v, ClickedListener listener) {
        llRight.removeAllComponents();
        llRight.addComponent(v);
        if (listener != null) {
            v.setClickedListener(listener);
        }
    }

    /**
     * 显示默认的左边的按钮
     */
    public void showBackImg(boolean show) {
        if (show) {
            llLeft.setVisibility(VISIBLE);
            setLeftIcon(ResourceTable.Media_ic_back_black, v -> {
                Context ctx = ActionBar.this.getContext();
                if (ctx instanceof Ability) {
                    ((Ability) ctx).terminateAbility();
                }
            });
        } else {
            llLeft.setVisibility(INVISIBLE);
        }
    }

    /**
     * ActionBar整个替换
     *
     * @param v v
     */
    public void setActionBarView(Component v) {
        llActionbar.removeAllComponents();
        llActionbar.addComponent(v);
    }

    /**
     * 获取textView
     */
    public Text getTextView() {
        Text tv = new Text(getContext());
        tv.setTextSize(centerTextSize);
        Color color = null;
        try {
            color = new Color(getResourceManager().getElement(centerTextColor).getColor());
            tv.setTextColor(color);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return tv;
    }

    /**
     * 获取imageView
     */
    public Image getImageView() {
        Image iv = new Image(getContext());
        iv.setLayoutConfig(new LayoutConfig(PxUtils.vp2px(getContext(), 24), PxUtils.vp2px(getContext(), 24)));
        return iv;

    }
}
